﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WorkNodeFramework
{
    public class Column:IQueryExpression
    {
        ITable table;
        string name;
        string alias;
        internal Column(ITable table, string Name) {
            this.name = Name;
            this.table = table;
        }

        public Column Alias(string alias) {
            Column col = new Column(table, name);
            col.alias = alias;
            return col;
        }

        public string Name { 
            get { 
                return name; 
            } 
        }

        bool isPrimaryKey = false;
        public bool IsPrimaryKey{
            get {
                return isPrimaryKey;
            }
            internal set {
                isPrimaryKey = value;
            }
        }

        bool isAutocrement = false;
        public bool IsAutocrement {
            get {
                return isAutocrement;
            }
            internal set {
                isAutocrement = value;
            }
        }

        bool isNullable = false;
        public bool IsNullable {
            get {
                return isNullable;
            }
            internal set {
                isNullable = value;
            }
        }

        object defaultValue;
        public object DefaultValue {
            get {
                return defaultValue;
            }
            internal set {
                defaultValue = value;
            }
        }

        public string ColumnAlias {
            get {
                return alias;
            }
        }

        public string FullName {
            get {
                return Table.Provider.QualifyColumnName(this);
            }
        }

        public ITable Table {
            get {
                return table;
            }
        }

        SqlDbType dbType = SqlDbType.VarChar;
        public SqlDbType SqlDbType {
            get {
                return dbType;
            }
            set {
                dbType = value;
            }
        }

        public Type DataType {
            get {
                return table.Provider.TableProvider.GetDataType(dbType);
            }
        }

        int size = -1;
        public int Size {
            get {
                return size;
            }
            set {
                size = value;
            }
        }

        #region IQueryExpression Members

        public string Expression
        {
            get {
                return DataService.Provider.QueryExpressioṇ̣̣̣̣́(this);
            }
        }

        #endregion

        public bool Equals(Column column)
        {
            return string.Compare(this.FullName, column.FullName, true) == 0;
        }

        public override bool Equals(object obj)
        {
            if (obj is Column)
                return Equals((Column)obj);
            return base.Equals(obj);
        }

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(name))
                return name;
            return base.ToString();
        }
    }
}
